package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.event;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.os.Build;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.graduationdesign.zengyan.graduationdesignapp.BaseApplication;
import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.base.Result;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Event;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.EventResultModel;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.SignUpResult;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.DataGenerator;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.commonlyUsed.SlideCardAdapter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseUiView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.ProfileContact;
import com.graduationdesign.zengyan.graduationdesignapp.ui.presenter.ProfilePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZengYan on 2018/4/22.
 *
 * @desc :
 */
public class EventFragment extends BaseFragment implements ProfileContact.ShowAllEvents, ProfileContact.ShowSignUpResult {

    @BindView(R.id.view_pager)
    ViewPager viewPager;

    @BindView(R.id.base_ui)
    BaseUiView baseUiView;

    @BindView(R.id.refresh)
    View refresh;

    private List<EventResultModel> mData = new ArrayList<EventResultModel>();


    private ProfileContact.Presenter presenter;

    private int color_array[] = {
            R.color.deep_orange_800,
            R.color.blue_500,
            R.color.amber_800,
            R.color.deep_purple_500
    };
    private SlideCardAdapter<EventResultModel> slideCardAdapter;


    @Override
    protected int provideLayoutId() {
        return R.layout.event_layout;
    }

    @Override
    protected String provideTag() {
        return "EventFragment";
    }

    @Override
    protected void initData() {
        BaseErrorView mErrorView = new CommonErrorView(getActivity());

        presenter = PresenterInjection.provideProfilePresenter(

                ProfilePresenter.newBuilder()
                        .setShowAllEvents(this)
                        .setMLoadingView(baseUiView)
                        .setBaseErrorView(mErrorView)
                        .setShowSignUpResult(this)

        );
        presenter.loadAllEvents();

    }

    @Override
    protected void initView(View view) {

        refresh.setOnClickListener(v -> {
            presenter.loadAllEvents();
        });

        slideCardAdapter = new SlideCardAdapter<EventResultModel>(getActivity(),R.layout.item_slice_event_card,mData) {
            @TargetApi(Build.VERSION_CODES.M)
            @Override
            public void bind(View rootview, EventResultModel eventResultModel, int position) {
                RelativeLayout mLytParent;
                CardView mCard;
                TextView mTitle,mDesc;
                ImageView mBg;
                Button mSignUp;
                mCard = (CardView) rootview.findViewById(R.id.card);
                mTitle = (TextView) rootview.findViewById(R.id.title);
                mDesc = (TextView) rootview.findViewById(R.id.desc);
                mBg = (ImageView) rootview.findViewById(R.id.bg);
                mSignUp = (Button) rootview.findViewById(R.id.signup_btn);

                Event data = eventResultModel.getEvent();


                mCard.setOnClickListener(v -> {
                    getContext().startActivity(UiInjection.provideEventDetail(getContext(), eventResultModel));
                });

                mTitle.setText(data.getTitle());
                List<Integer> integers = DataGenerator.getNatureImages(getContext());
                mBg.setBackgroundResource(integers.get(DataGenerator.randInt(integers.size()-1)));

                if (eventResultModel.isSignUp()) {
                    mSignUp.setText("已报名");
                    mSignUp.setTextColor(BaseApplication.Instance.getColor(R.color.overlay_light_40));
                }else {
                    mSignUp.setOnClickListener(v -> {
                        presenter.signUp("201430098034",data.getId());
                    });
                }

            }
        };


        viewPager.setAdapter(slideCardAdapter);

        viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.viewpager_layout_margin_overlap_payment));
        viewPager.setOffscreenPageLimit(slideCardAdapter.getCount());

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.unSubsrible();
    }

    @Override
    public void onVisible() {
        super.onVisible();
        presenter.loadAllEvents();
    }

    @Override
    public void showAllEvents(List<EventResultModel> events) {
        mData.clear();
        mData.addAll(events);
        slideCardAdapter.notifyDataSetChanged();
    }

    @Override
    public void showSignUpResult(Result<SignUpResult> result) {


        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
        dialog.setContentView(R.layout.dialog_signup);
        dialog.setCancelable(true);

        ((AppCompatButton) dialog.findViewById(R.id.bt_close)).setOnClickListener(v -> dialog.dismiss());
        dialog.setOnDismissListener(dialog1 -> {  presenter.loadAllEvents();});

        dialog.show();
    }
}
